const db = wx.cloud.database();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    act: 11,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    var q1 = wx.getStorageSync('q1')
    var q2 = wx.getStorageSync('q2')
    var q3 = wx.getStorageSync('q3')
    var q4 = wx.getStorageSync('q4')
    var q5 = wx.getStorageSync('q5')
    var q11 = wx.getStorageSync('q11')
    var q22 = wx.getStorageSync('q22')
    var tip = wx.getStorageSync('tip')
    console.log(options);
    _.setData({
      q1,
      q2,
      q3,
      q4,
      q5,
      q11,
      q22,
      tip,
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
  },
  close(e) {
    var _ = this;
    wx.setStorageSync('tip', '1')
    _.setData({
      tip: 1
    })
  },
  add(e) {
    var _ = this;
    var id = e.currentTarget.dataset.id;
    var phone = wx.getStorageSync('riliphone')
    if (phone) {
      if (id == '1') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送公告发布时间提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q1', '1')
              _.setData({
                q1: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['IDSJ5gZMvZGGvSRLPxKCKx02WW1TiXzFIWqSUEKmBzE'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '公告发布1',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '2') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送省考报名时间提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q2', '1')
              _.setData({
                q2: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['GFqJYmcnvqp_uIjIFLtZFo9az1HnXr-JKmbwdV-C1tE'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '省考报名2',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '3') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送准考证打印时间提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q3', '1')
              _.setData({
                q3: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['rJ6W2cvKFSNBdlrwCN0AXi9yBkfb9ZZYPd8qK9YxsRM'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '准考证打印3',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '4') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送笔试时间提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q4', '1')
              _.setData({
                q4: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['FHuriu5KRom3hI3gXHA5l4VnxjVZQAkosynrv9m2nl8'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '省考笔试4',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '11') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送资格审查提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q11', '1')
              _.setData({
                q11: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['tzpnx2tykHOYtbJXudRd-FkqIfswgceDlYN2VgBffYE'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '资格审查11',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (id == '22') {
        wx.showModal({
          title: '提示',
          content: '是否需要推送缴费提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q22', '1')
              _.setData({
                q22: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['DVOiaM6AvncG_ctQLN3X4kP26KhrMrvNO795-TC4oj0'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '缴费22',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '是否需要推送缴费提醒',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.setStorageSync('q22', '1')
              _.setData({
                q22: 1
              })
              wx.requestSubscribeMessage({
                tmplIds: ['DVOiaM6AvncG_ctQLN3X4kP26KhrMrvNO795-TC4oj0'], //笔试时间
                success(res) {
                  console.log(res);
                  db.collection('userInfo').where({
                    _openid: app.globalData.openid,
                  }).get({
                    success: res => {
                      var _ = this;
                      db.collection('userInfo').add({
                        data: {
                          xiangmu: '缴费22',
                          phone: phone
                        },
                        //添加成功的回调函数
                        success(res) {

                          wx.showToast({
                            title: '预约成功！',
                          })
                          console.log('cloud get success!')
                        }
                      });
                    },
                  })
                },
                fail(res) {
                  console.log(res);
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }


    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      _.setData({
        login: 1
      })
    }
  },
  getPhoneNumber(e) {
    var _ = this;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      _.setData({
        mobile: res.result,
      })
      console.log(res.result)
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 39,
          mobile: res.result,
          areaCode: _.data.area,
          agent: _.data.geneal,
          pageId: _.data.page
        },
        success: res => {
          console.log(res);
        }
      })

      wx.setStorageSync('riliphone', _.data.mobile)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/biaodan/register?actid=43505&callback=?', //接收url
        data: {
          phone: _.data.mobile,
          fenxiao: _.data.area,
          geneal: _.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          _.setData({
            login: 0
          })
        }
      });

    })

  },
  litap(e) {
    var _ = this;
    console.log(e.currentTarget.dataset.act)
    var act = e.currentTarget.dataset.act;
    _.setData({
      act
    })
  },
  choiseday(e) {
    var _ = this;
    console.log(e.currentTarget.dataset.day)
    var day = e.currentTarget.dataset.day;
    _.setData({
      day
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

/**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _ = this;
    var area = wx.getStorageSync('area')
    var agent = wx.getStorageSync('agent')
    var page = wx.getStorageSync('page')
    return {
      title: '2022山东省省考百宝箱',
      path: 'pages/index/index?area=' + area + '&agent=' + agent + '&page=' + page
    }

  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    var _ = this;
    var area = wx.getStorageSync('area')
    var agent = wx.getStorageSync('agent')
    var page = wx.getStorageSync('page')
    return {
      title: '2022山东省省考百宝箱',
      path: 'pages/index/index?area=' + area + '&agent=' + agent + '&page=' + page
    }
  }

})